#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=..\icon.ico
#AutoIt3Wrapper_Compile_Both=y
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Res_Fileversion=0.0.1.1
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=p
#AutoIt3Wrapper_Res_Language=1040
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#AutoIt3Wrapper_Run_After=move "main.exe" "..\MetroLaunch.exe"
#AutoIt3Wrapper_Run_After=move "main_x64.exe" "..\MetroLaunch_x64.exe"
#AutoIt3Wrapper_Run_Tidy=y
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <StructureConstants.au3>
#include <GuiScrollBars.au3>
#include <ScrollBarConstants.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <String.au3>

#include "GUIScrollbars_Ex.au3"
#include "WinAPIEx.au3"
#include "APIConstants.au3"

#include "graphic.au3"
#include "metrocontrol.au3"
#include "tiles.au3"
#include "common.au3"

Opt("GUIOnEventMode", 1)

#region Startup
_GDIPlus_Startup()
If Not FileExists(@ScriptDir & "\settings.ini") Then
	Global $bFirstStart = True
	_CreateSettings()
Else
	Global $bFirstStart = False
	_LoadSettings()
EndIf
_LoadTranslation($Settings_Language)
If $bFirstStart = True Then
	Local $hChoice = MsgBox(68, $aLang[46], $aLang[51])
	If $hChoice = 6 Then
		_FontInstall(@ScriptDir & "\segoeuisl_0.ttf")
	EndIf
EndIf
Global $sBackground = $Settings_Background
#endregion Startup

#region Main GUI
Global $hMainGUI = GUICreate("Metro Launch", @DesktopWidth, @DesktopHeight, @DesktopWidth, 0, $WS_POPUP)
If StringLeft($sBackground, 2) = "0x" Then
	GUISetBkColor($sBackground, $hMainGUI)
Else
	GUICtrlCreatePic($sBackground, 0, 0, @DesktopWidth, @DesktopHeight)
	GUICtrlSetState(-1, $GUI_DISABLE)
EndIf

_MetroUI_CreateLabel($hMainGUI, $aLang[0], 40, 40, 160, 64, 42, 0xffffff)
GUICtrlCreateLabel(@UserName, @DesktopWidth - 300, 40, 160, 64, BitOR($SS_NOTIFY, $SS_RIGHT))
GUICtrlSetFont(-1, 24, 200, 0, "Segoe UI Semilight")
GUICtrlSetColor(-1, 0xffffff)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
GUICtrlCreateLabel(@ComputerName, @DesktopWidth - 300, 80, 160, 64, BitOR($SS_NOTIFY, $SS_RIGHT))
GUICtrlSetFont(-1, 16, 200, 0, "Segoe UI Semilight")
GUICtrlSetColor(-1, 0xffffff)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
#endregion Main GUI

#region Tiles Container
Global $hTiles = GUICreate("Tiles", @DesktopWidth, @DesktopHeight, 3, 3, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_MDICHILD), $hMainGUI)
GUISetBkColor(0x000001)
#endregion Tiles Container

#region Charm Bar
Global $hCharmBar = GUICreate("Charm Bar", $iCharmBarWidth, @DesktopHeight, @DesktopWidth - $iCharmBarWidth, 0, $WS_POPUP, Default, $hMainGUI)
WinSetOnTop($hCharmBar, "", 1)

Global $hMainGraphic = _GDIPlus_GraphicsCreateFromHWND($hMainGUI)
Global $hCharmGraphic = _GDIPlus_GraphicsCreateFromHWND($hCharmBar)

GUICtrlCreateLabel("", $iCharmBarWidth / 2 - 24, @DesktopHeight / 2 - 160, 48, 48)
GUICtrlSetOnEvent(-1, "_add")
GUICtrlCreateLabel($aLang[1], 10, @DesktopHeight / 2 - 160 + 50, 60, Default, $SS_CENTER)
GUICtrlSetFont(-1, 10, 600, 0, "Segoe UI Semilight")
GUICtrlSetColor(-1, 0xffffff)
GUICtrlSetOnEvent(-1, "_Add")
GUICtrlCreateLabel("", $iCharmBarWidth / 2 - 24, @DesktopHeight / 2 - 80, 48, 48)
GUICtrlSetOnEvent(-1, "_Pin")
GUICtrlCreateLabel($aLang[2], 10, @DesktopHeight / 2 - 80 + 50, 60, Default, $SS_CENTER)
GUICtrlSetFont(-1, 10, 600, 0, "Segoe UI Semilight")
GUICtrlSetColor(-1, 0xffffff)
GUICtrlSetOnEvent(-1, "_Pin")
GUICtrlCreateLabel("", $iCharmBarWidth / 2 - 24, @DesktopHeight / 2, 48, 48)
GUICtrlSetOnEvent(-1, "_Settings")
GUICtrlCreateLabel($aLang[3], 10, @DesktopHeight / 2 + 50, 60, Default, $SS_CENTER)
GUICtrlSetFont(-1, 10, 600, 0, "Segoe UI Semilight")
GUICtrlSetColor(-1, 0xffffff)
GUICtrlSetOnEvent(-1, "_Settings")
GUICtrlCreateLabel("", $iCharmBarWidth / 2 - 24, @DesktopHeight / 2 + 80, 48, 48)
GUICtrlSetOnEvent(-1, "_Exit")
GUICtrlCreateLabel($aLang[4], 10, @DesktopHeight / 2 + 80 + 50, 60, Default, $SS_CENTER)
GUICtrlSetFont(-1, 10, 600, 0, "Segoe UI Semilight")
GUICtrlSetColor(-1, 0xffffff)
GUICtrlSetOnEvent(-1, "_Exit")

GUISetBkColor(0x000000, $hCharmBar)
GUIRegisterMsg($WM_PAINT, "MY_WM_PAINT")
#endregion Charm Bar

#region Add Bar
Global $hAddBar = GUICreate("Add Bar", $iCharmBarWidth, @DesktopHeight, @DesktopWidth - $iCharmBarWidth, 0, $WS_POPUP, Default, $hMainGUI)
WinSetOnTop($hAddBar, "", 1)

Global $hAddGraphics = _GDIPlus_GraphicsCreateFromHWND($hAddBar)

GUICtrlCreateLabel("", $iCharmBarWidth / 2 - 24, @DesktopHeight / 2 - 160, 48, 48)
GUICtrlSetOnEvent(-1, "_AddBar_Lockscreen")
GUICtrlCreateLabel($aLang[48], 10, @DesktopHeight / 2 - 160 + 50, 60, Default, $SS_CENTER)
GUICtrlSetFont(-1, 10, 600, 0, "Segoe UI Semilight")
GUICtrlSetColor(-1, 0xffffff)
GUICtrlSetOnEvent(-1, "_AddBar_Lockscreen")
GUICtrlCreateLabel("", $iCharmBarWidth / 2 - 24, @DesktopHeight / 2 - 80, 48, 48)
GUICtrlSetOnEvent(-1, "_AddBar_Desktop")
GUICtrlCreateLabel($aLang[49], 10, @DesktopHeight / 2 - 80 + 50, 60, Default, $SS_CENTER)
GUICtrlSetFont(-1, 10, 600, 0, "Segoe UI Semilight")
GUICtrlSetColor(-1, 0xffffff)
GUICtrlSetOnEvent(-1, "_AddBar_Desktop")
GUICtrlCreateLabel("", $iCharmBarWidth / 2 - 24, @DesktopHeight / 2 + 80, 48, 48)
GUICtrlSetOnEvent(-1, "_AddBar_Exit")
GUICtrlCreateLabel($aLang[50], 10, @DesktopHeight / 2 + 80 + 50, 60, Default, $SS_CENTER)
GUICtrlSetFont(-1, 10, 600, 0, "Segoe UI Semilight")
GUICtrlSetColor(-1, 0xffffff)
GUICtrlSetOnEvent(-1, "_AddBar_Exit")

GUISetBkColor(0x000000, $hAddBar)
GUIRegisterMsg($WM_PAINT, "MY_WM_PAINT")
#endregion Add Bar

#region Loading
Global $hLoading = GUICreate("Metro Launch Loading", @DesktopWidth, 16, 0, 0, $WS_POPUP, $WS_EX_LAYERED, $hTiles)
If StringLeft($sBackground, 2) = "0x" Then
	GUISetBkColor($sBackground, $hLoading)
Else
	Global $hBackground = GUICtrlCreatePic($sBackground, 0, 0, @DesktopWidth, @DesktopHeight)
	GUICtrlSetState(-1, $GUI_DISABLE)
EndIf
#endregion Loading

GUISetState(@SW_SHOW, $hMainGUI)
GUISetState(@SW_SHOW, $hTiles)
_WinAPI_SetLayeredWindowAttributes($hTiles, 0x000001, 255)

GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit", $hMainGUI)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit", $hCharmBar)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit", $hLoading)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit", $hTiles)

_GUI_MoveFull($hMainGUI, Random(1, 4, 1))

Global $hTilesInit = _Tiles_Init($hTiles, $iTilesZoneX, $iTilesZoneY, $iMaxTiles)
GUISwitch($hTiles)
For $i = 1 To $Settings_Tiles[0][0] Step 1
	Local $aInfo = IniRead(@ScriptDir & "\settings.ini", "tiles", $Settings_Tiles[$i][0], "")
	Local $aData = StringSplit($aInfo, "|")
	If $aData[0] = 6 Then
		Local $hTile = _Tiles_AddTile($hTilesInit, $i - 1, $aData[1], $aData[2], $aData[3], $aData[4], $aData[5], $aData[6])
	EndIf
Next
GUISetState(@SW_SHOW, $hLoading)

GUISwitch($hMainGUI)

Global $hLoadInit = _Load_Init($hLoading, 0, 0, @DesktopWidth)
Global $hLoadCircles = _Load_SetCirclesNumber(5)
GUISetState(@SW_SHOW, $hCharmBar)

Global $hScrollbar = _GUIScrollbars_Generate($hTiles, ($iWidthUnit * $Settings_Tiles[0][0] + $iWidthUnit * ($Settings_Tiles[0][0] - 1)))

While 1
	Local $aCharmPos = WinGetPos($hCharmBar)
	If MouseGetPos(0) >= @DesktopWidth - $iCharmBarWidth And $aCharmPos[0] <> @DesktopWidth - $iCharmBarWidth And MouseGetPos(1) <= @DesktopHeight - 20 And (WinActive($hMainGUI) Or WinActive($hCharmBar) Or WinActive($hAddBar) Or WinActive($hTiles)) Then
		For $i3 = @DesktopWidth To @DesktopWidth - $iCharmBarWidth Step -10
			WinMove($hCharmBar, "", $i3, 0)
			Sleep(10)
		Next
	ElseIf $aCharmPos[0] <> @DesktopWidth And (MouseGetPos(0) < @DesktopWidth - $iCharmBarWidth Or MouseGetPos(1) >= @DesktopHeight - 20) Then
		For $i4 = @DesktopWidth - $iCharmBarWidth To @DesktopWidth Step 10
			WinMove($hCharmBar, "", $i4, 0)
			Sleep(10)
		Next
	EndIf
WEnd

Func _Exit()
	AdlibUnRegister("_Loading")
	If IsDeclared("hLoadInit") And IsDeclared("hLoadCircles") Then _Load_Shutdown($hLoadInit, $hLoadCircles)
	_GDIPlus_GraphicsDispose($hMainGraphic)
	_GDIPlus_GraphicsDispose($hCharmGraphic)
	_DisposeImg()
	_GDIPlus_Shutdown()
	For $i = 1 To $Settings_Tiles[0][0] Step 1
		IniDelete(@ScriptDir & "\settings.ini", "tiles", $i)
	Next
	For $i = 0 To UBound($aTiles) - 1 Step 1
		If $aTiles[$i][0] <> 0 Then
			Local $aPos = _Tiles_TileGetPos($aTiles[$i][0])
			IniWrite(@ScriptDir & "\settings.ini", "tiles", $i + 1, $aPos[0] & "|" & $aPos[1] & "|" & $aTiles[$i][4] & "|" & $aTiles[$i][2] & "|" & $aTiles[$i][1] & "|" & $aTiles[$i][3])
		EndIf
	Next
	If IniRead(@ScriptDir & "\settings.ini", "tiles", "1", "Error") = "Error" Then IniWrite(@ScriptDir & "\settings.ini", "tiles", "1", "")
	GUIDelete($hLoading)
	Local $aWinPos = WinGetPos($hMainGUI)
	For $i = 0 To @DesktopWidth - $aWinPos[2] / 700 Step 30
		WinMove($hMainGUI, "", $i, 0)
		Sleep(10)
	Next
	For $i2 = @DesktopWidth - $aWinPos[2] / 700 To @DesktopWidth Step 10
		WinMove($hMainGUI, "", $i2, 0)
		Sleep(10)
	Next
	Exit
EndFunc   ;==>_Exit

#region Charm Bar
Func _Settings()
	If @OSArch = "X86" Then
		Run(@ScriptDir & "\MLSettings.exe")
	Else
		Run(@ScriptDir & "\MLSettings_x64.exe")
	EndIf
EndFunc   ;==>_Settings

Func _Pin()
	Local $bError = False
	Local $hSelect = FileOpenDialog($aLang[5], @DesktopDir, $aLang[6] & " (*.*)", 1 + 2)
	If @error Then $bError = True
	Local $aName = StringSplit($hSelect, "\")
	Local $aNewPos[2] = [0, 0]
	For $i = 0 To UBound($aTiles) - 1 Step 1
		If $aTiles[$i][6] > $aNewPos[0] Then
			$aNewPos[0] = $aTiles[$i][6] + 1
		ElseIf $aTiles[$i][6] = $aNewPos[0] Then
			$aNewPos[0] = $aTiles[$i][6] + 1
		EndIf
		If $aTiles[$i][7] > $aNewPos[1] Then
			$aNewPos[1] = $aTiles[$i][7] + 1
		ElseIf $aTiles[$i][7] = $aNewPos[1] Then
			$aNewPos[1] = $aTiles[$i][7] + 1
		EndIf
	Next
	If $bError = False Then
		_Tiles_AddTile($hTilesInit, Number($Settings_Tiles[0][0]), $aNewPos[0], $aNewPos[1], 1, 0xffffff, $hSelect, $aName[$aName[0]])
	EndIf
EndFunc   ;==>_Pin
#endregion Charm Bar

#region Add Bar
Func _Add()
	GUISetState(@SW_SHOW, $hAddBar)
	For $i = @DesktopWidth To @DesktopWidth - $iCharmBarWidth Step -10
		WinMove($hAddBar, "", $i, 0)
		Sleep(10)
	Next
EndFunc   ;==>_Add

Func _AddBar_Lockscreen()
	Local $bError = False
	If @error Then $bError = True
	Local $aNewPos[2] = [0, 0]
	For $i = 0 To UBound($aTiles) - 1 Step 1
		If $aTiles[$i][6] > $aNewPos[0] Then
			$aNewPos[0] = $aTiles[$i][6] + 1
		ElseIf $aTiles[$i][6] = $aNewPos[0] Then
			$aNewPos[0] = $aTiles[$i][6] + 1
		EndIf
		If $aTiles[$i][7] > $aNewPos[1] Then
			$aNewPos[1] = $aTiles[$i][7] + 1
		ElseIf $aTiles[$i][7] = $aNewPos[1] Then
			$aNewPos[1] = $aTiles[$i][7] + 1
		EndIf
	Next
	If $bError = False Then
		_Tiles_AddTile($hTilesInit, Number($Settings_Tiles[0][0]), $aNewPos[0], $aNewPos[1], 1, 0xffffff, "lockscreen", _StringProper($aLang[48]))
	EndIf
EndFunc   ;==>_AddBar_Lockscreen

Func _AddBar_Desktop()
	Local $bError = False
	If @error Then $bError = True
	Local $aNewPos[2] = [0, 0]
	For $i = 0 To UBound($aTiles) - 1 Step 1
		If $aTiles[$i][6] > $aNewPos[0] Then
			$aNewPos[0] = $aTiles[$i][6] + 1
		ElseIf $aTiles[$i][6] = $aNewPos[0] Then
			$aNewPos[0] = $aTiles[$i][6] + 1
		EndIf
		If $aTiles[$i][7] > $aNewPos[1] Then
			$aNewPos[1] = $aTiles[$i][7] + 1
		ElseIf $aTiles[$i][7] = $aNewPos[1] Then
			$aNewPos[1] = $aTiles[$i][7] + 1
		EndIf
	Next
	If $bError = False Then
		_Tiles_AddTile($hTilesInit, Number($Settings_Tiles[0][0]), $aNewPos[0], $aNewPos[1], 1, 0xffffff, "desktop", _StringProper($aLang[49]))
	EndIf
EndFunc   ;==>_AddBar_Desktop

Func _AddBar_Exit()
	For $i4 = @DesktopWidth - $iCharmBarWidth To @DesktopWidth Step 10
		WinMove($hAddBar, "", $i4, 0)
		Sleep(10)
	Next
	GUISetState(@SW_HIDE, $hAddBar)
EndFunc   ;==>_AddBar_Exit
#endregion Add Bar

#region Auxiliary functions
Func _FontInstall($sFile)
	Local $Font, $Name, $Path

	$Name = _WinAPI_GetFontResourceInfo($sFile, 1)
	If @error Then
		Return SetError(1, 0, 0)
	EndIf
	$Name &= ' (TrueType)'
	$Font = StringRegExpReplace($sFile, '^.*\\', '')
	If Not RegWrite('HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts', $Name, 'REG_SZ', $Font) Then
		Return SetError(2, 0, 0)
	EndIf
	$Path = _WinAPI_ShellGetSpecialFolderPath($CSIDL_FONTS)
	If Not FileCopy($sFile, $Path) Then
		RegDelete('HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts', $Name)
		Return SetError(3, 0, 0)
	EndIf
	If Not _WinAPI_AddFontResourceEx($Path & '\' & $Font, 0, 1) Then
		Return SetError(4, 0, 0)
	EndIf
	Return 1
EndFunc   ;==>_FontInstall

Func _Loading()
	_Load_Start($hLoadInit, $hLoadCircles)
EndFunc   ;==>_Loading

Func MY_WM_PAINT($hWnd, $msg, $wParam, $lParam)
	#forceref $hWnd, $Msg, $wParam, $lParam
	#region Context Menu
	_WinAPI_RedrawWindow($hContext, 0, 0, $RDW_UPDATENOW)

	_GDIPlus_GraphicsClear($hContextGraphics)
	_GDIPlus_GraphicsDrawImage($hContextGraphics, $hPNG_Delete, @DesktopWidth / 2 - 96, 8)
	Local $aWinPos = WinGetPos($hRClickedTile)
	If IsArray($aWinPos) Then
		If Round(($aWinPos[2] - $iSpace) / $iWidthUnit) > 1 Then
			_GDIPlus_GraphicsDrawImage($hContextGraphics, $hPNG_Restore, @DesktopWidth / 2 - 32, 8)
		Else
			_GDIPlus_GraphicsDrawImage($hContextGraphics, $hPNG_Enlarge, @DesktopWidth / 2 - 32, 8)
		EndIf
	EndIf
	_GDIPlus_GraphicsDrawImage($hContextGraphics, $hPNG_Rename, @DesktopWidth / 2 + 32, 8)
	_GDIPlus_GraphicsDrawImage($hContextGraphics, $hPNG_ChangePic, @DesktopWidth / 2 + 96, 8)
	_WinAPI_RedrawWindow($hContext, 0, 0, $RDW_VALIDATE)
	#endregion Context Menu
	#region Charm Bar
	_WinAPI_RedrawWindow($hCharmBar, 0, 0, $RDW_UPDATENOW)

	_GDIPlus_GraphicsDrawImage($hCharmGraphic, $hPNG_AddApp, $iCharmBarWidth / 2 - 24, @DesktopHeight / 2 - 160)
	_GDIPlus_GraphicsDrawImage($hCharmGraphic, $hPNG_AddExe, $iCharmBarWidth / 2 - 24, @DesktopHeight / 2 - 80)
	_GDIPlus_GraphicsDrawImage($hCharmGraphic, $hPNG_Settings, $iCharmBarWidth / 2 - 24, @DesktopHeight / 2)
	_GDIPlus_GraphicsDrawImage($hCharmGraphic, $hPNG_Exit, $iCharmBarWidth / 2 - 24, @DesktopHeight / 2 + 80)

	_WinAPI_RedrawWindow($hCharmBar, 0, 0, $RDW_VALIDATE)
	#endregion Charm Bar
	#region Add Bar
	_WinAPI_RedrawWindow($hAddBar, 0, 0, $RDW_UPDATENOW)

	_GDIPlus_GraphicsDrawImage($hAddGraphics, $hPNG_Lockscreen, $iCharmBarWidth / 2 - 24, @DesktopHeight / 2 - 160)
	_GDIPlus_GraphicsDrawImage($hAddGraphics, $hPNG_Desktop, $iCharmBarWidth / 2 - 24, @DesktopHeight / 2 - 80)
	_GDIPlus_GraphicsDrawImage($hAddGraphics, $hPNG_Back, $iCharmBarWidth / 2 - 24, @DesktopHeight / 2 + 80)

	_WinAPI_RedrawWindow($hAddBar, 0, 0, $RDW_VALIDATE)
	#endregion Add Bar
	Return $GUI_RUNDEFMSG
EndFunc   ;==>MY_WM_PAINT
#endregion Auxiliary functions
